package com.cruel_wandering.wifi_geo_predictor;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SimpleCursorAdapter;

public class DBScreen extends ListActivity
{
	static private final String[] keys = {
    DataBase.KEY_ROWID, DataBase.KEY_BSSID, DataBase.KEY_RADIUS,
	  DataBase.KEY_AP_LOCATION_LAT, DataBase.KEY_AP_LOCATION_LON,
	  DataBase.KEY_AP_LOCATION_ALT
	};
	static private final String[] columns = {
		DataBase.KEY_BSSID, DataBase.KEY_RADIUS, DataBase.KEY_AP_LOCATION_LAT,
	  DataBase.KEY_AP_LOCATION_LON, DataBase.KEY_AP_LOCATION_ALT
	};
	static private final int[] to = {
		R.id.db_screen_bssid, R.id.db_screen_radius, R.id.db_screen_lat,
		R.id.db_screen_lon, R.id.db_screen_alt
	};

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.db_screen);

		
		DataBase db = DataBase.getInstance(this);
		db.open();
		Cursor cursor = db.getDatabase().query(true,
			    DataBase.DATABASE_TABLE, keys, null, null, /*DataBase.KEY_BSSID*/ null, null,
			    null, null);
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				R.layout.db_screen_entry, cursor, columns, to);
		this.setListAdapter(adapter);
	}

  @Override
  public boolean onCreateOptionsMenu(Menu menu)
  {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        menu.removeItem(R.id.db_screen_menu);

        menu.add(0, 1, 0, "Add new Wifi spot");
        return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item)
  {
          switch (item.getItemId())
          {
          case 1:
        	  startActivityForResult(new Intent(this, MapScreen.class), 0);
          case R.id.preferences:
          {
          	// Launch Preference activity
          	Intent i = new Intent(DBScreen.this, Preferences.class);
          	startActivity(i);
          	break;
          }
          case R.id.map_screen_menu:
          	startActivity(new Intent(DBScreen.this, MapScreen.class));
          	break;
          default:
          }
          return true;
  }
}